﻿package mortal.game.view.signUp.lineOn.item
{
    import com.gengine.core.frame.*;
    import com.mui.controls.*;
    import fl.events.*;
    import flash.utils.*;
    import mortal.game.cache.*;
    import mortal.game.resource.*;
    import mortal.game.view.common.*;
    import mortal.game.view.signUp.lineOn.data.*;

    public class LineOnGiftPanel extends GSprite
    {
        public var _leftBtn:GLoadedButton;
        public var _cellArr:Array;
        private var _cellContainer:GSprite;
        private var _maskSp:GSprite;
        public var _rightBtn:GLoadedButton;
        private var _moveTime:uint = 0;
        private var _modeDir:int = 1;
        private var oldX:int;
        private var tarX:int;
        private var _moveTimer:FrameTimer;
        private var cellWid:int = 157;

        public function LineOnGiftPanel()
        {
            this._moveTimer = new FrameTimer();
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this._leftBtn = UIFactory.gLoadedButton(ImagesConst.MountLeft_upSkin, 0, 100, 33, 37, this);
            this._rightBtn = UIFactory.gLoadedButton(ImagesConst.MountRight_upSkin, 653, 100, 33, 37, this);
            this._cellContainer = UIFactory.getUICompoment(GSprite) as GSprite;
            this._cellContainer.x = 31;
            this.addChild(this._cellContainer);
            this._maskSp = UIFactory.getUICompoment(GSprite) as GSprite;
            this._maskSp.graphics.clear();
            this._maskSp.graphics.beginFill(0);
            this._maskSp.graphics.drawRect(0, 0, 4 * this.cellWid, 246);
            this._maskSp.graphics.endFill();
            this._maskSp.x = this._cellContainer.x;
            this.addChild(this._maskSp);
            this._cellContainer.mask = this._maskSp;
            var _loc_1:* = LineOnRewardConfig.instance.rewardInfoArr.length;
            this.genCells(_loc_1);
            this._leftBtn.configEventListener(ComponentEvent.BUTTON_DOWN, this.onBtnDownHd);
            this._rightBtn.configEventListener(ComponentEvent.BUTTON_DOWN, this.onBtnDownHd);
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            super.disposeImpl(param1);
            this._leftBtn.autoRepeat = false;
            this._rightBtn.autoRepeat = false;
            this._leftBtn.dispose(param1);
            this._rightBtn.dispose(param1);
            this._leftBtn = null;
            this._rightBtn = null;
            this.remvoeCells();
            this._cellContainer.mask = null;
            this._cellContainer.dispose(param1);
            this._cellContainer = null;
            this._maskSp.dispose(param1);
            this._maskSp.graphics.clear();
            this._maskSp = null;
            return;
        }// end function

        private function genCells(param1:int) : void
        {
            var _loc_3:LineOnCell = null;
            this._cellArr = [];
            var _loc_2:int = 0;
            while (_loc_2 < param1)
            {
                
                _loc_3 = UIFactory.getUICompoment(LineOnCell) as LineOnCell;
                _loc_3.x = _loc_2 * this.cellWid;
                _loc_3.y = 0;
                this._cellArr.push(_loc_3);
                this._cellContainer.addChild(_loc_3);
                _loc_2++;
            }
            if (param1 > 4)
            {
                var _loc_4:Boolean = true;
                this._rightBtn.visible = true;
                this._leftBtn.visible = _loc_4;
            }
            else
            {
                var _loc_4:Boolean = false;
                this._rightBtn.visible = false;
                this._leftBtn.visible = _loc_4;
            }
            return;
        }// end function

        private function remvoeCells() : void
        {
            var _loc_2:LineOnCell = null;
            if (this._cellArr == null)
            {
                return;
            }
            var _loc_1:int = 0;
            while (_loc_1 < this._cellArr.length)
            {
                
                _loc_2 = this._cellArr[_loc_1];
                _loc_2.dispose();
                _loc_1++;
            }
            this._cellArr = null;
            return;
        }// end function

        public function initCells() : void
        {
            var _loc_2:LineOnCell = null;
            var _loc_3:Object = null;
            if (this._cellArr == null)
            {
                return;
            }
            var _loc_1:int = 0;
            while (_loc_1 < this._cellArr.length)
            {
                
                _loc_2 = this._cellArr[_loc_1];
                _loc_3 = LineOnRewardConfig.instance.rewardInfoArr[_loc_1];
                _loc_2.initCell(_loc_3);
                _loc_1++;
            }
            this.scorllToLastCanGetCell();
            return;
        }// end function

        public function updateOneCell(param1:Object) : void
        {
            var _loc_3:LineOnCell = null;
            var _loc_2:int = 0;
            while (_loc_2 < this._cellArr.length)
            {
                
                _loc_3 = this._cellArr[_loc_2];
                if (_loc_3.updateAfterGet(param1))
                {
                }
                _loc_2++;
            }
            return;
        }// end function

        private function onBtnDownHd(event:ComponentEvent) : void
        {
            var _loc_3:int = 0;
            var _loc_4:int = 0;
            if (this._leftBtn == event.currentTarget)
            {
                if (this._cellContainer.x + 10 >= this._maskSp.x)
                {
                    return;
                }
                this._modeDir = 1;
            }
            else
            {
                _loc_3 = this._cellArr.length * this.cellWid;
                _loc_4 = this._maskSp.x - (_loc_3 - 4 * this.cellWid);
                if (this._cellContainer.x - 10 <= _loc_4)
                {
                    return;
                }
                this._modeDir = -1;
            }
            var _loc_2:* = this._cellContainer.x;
            this.tarX = this._cellContainer.x + this.cellWid * this._modeDir;
            this._moveTimer.addListener(TimerType.ENTERFRAME, this.onEnterT);
            this._moveTimer.start();
            var _loc_5:Boolean = false;
            this._rightBtn.enabled = false;
            this._leftBtn.enabled = _loc_5;
            return;
        }// end function

        private function onEnterT(param1:FrameTimer) : void
        {
            this._cellContainer.x = this._cellContainer.x + this._modeDir * 10;
            if (this._modeDir > 0)
            {
                if (this._cellContainer.x >= this.tarX)
                {
                    this._cellContainer.x = this.tarX;
                    this._moveTimer.stop();
                    this.checkPos();
                    var _loc_2:Boolean = true;
                    this._rightBtn.enabled = true;
                    this._leftBtn.enabled = _loc_2;
                }
            }
            else if (this._cellContainer.x <= this.tarX)
            {
                this._cellContainer.x = this.tarX;
                this._moveTimer.stop();
                this.checkPos();
                var _loc_2:Boolean = true;
                this._rightBtn.enabled = true;
                this._leftBtn.enabled = _loc_2;
            }
            return;
        }// end function

        private function checkPos() : void
        {
            clearTimeout(this._moveTime);
            this._moveTime = 0;
            var _loc_1:* = this._cellArr.length * this.cellWid;
            var _loc_2:* = this._maskSp.x - (_loc_1 - 4 * this.cellWid);
            this._cellContainer.x = this._cellContainer.x < _loc_2 ? (_loc_2) : (this._cellContainer.x);
            this._cellContainer.x = this._cellContainer.x > this._maskSp.x ? (this._maskSp.x) : (this._cellContainer.x);
            return;
        }// end function

        public function updateCellTime() : void
        {
            var _loc_3:LineOnCell = null;
            var _loc_4:int = 0;
            var _loc_1:* = Cache.instance.lineOn.timeLeftArr;
            var _loc_2:int = 0;
            while (_loc_2 < this._cellArr.length)
            {
                
                _loc_3 = this._cellArr[_loc_2];
                _loc_4 = _loc_1[_loc_2];
                _loc_3.updateContinueMinu(_loc_4);
                _loc_2++;
            }
            return;
        }// end function

        public function hasGift() : Boolean
        {
            var _loc_3:LineOnCell = null;
            var _loc_4:int = 0;
            var _loc_1:* = Cache.instance.lineOn.timeLeftArr;
            var _loc_2:int = 0;
            while (_loc_2 < this._cellArr.length)
            {
                
                _loc_3 = this._cellArr[_loc_2];
                _loc_4 = _loc_1[_loc_2];
                if (_loc_3.hasGift(_loc_4))
                {
                    return true;
                }
                _loc_2++;
            }
            return false;
        }// end function

        private function scorllToLastCanGetCell() : void
        {
            var _loc_1:* = Cache.instance.lineOn.findCanGetIndex();
            this._cellContainer.x = this._maskSp.x + _loc_1 * (-this.cellWid);
            this.checkPos();
            return;
        }// end function

        public function scrollRightOneCell() : void
        {
            var _loc_1:* = this._cellArr.length * this.cellWid;
            var _loc_2:* = this._maskSp.x - (_loc_1 - 4 * this.cellWid);
            if (this._cellContainer.x - 10 <= _loc_2)
            {
                return;
            }
            this._modeDir = -1;
            var _loc_3:* = this._cellContainer.x;
            this.tarX = this._cellContainer.x + this.cellWid * this._modeDir;
            this._moveTimer.addListener(TimerType.ENTERFRAME, this.onEnterT);
            this._moveTimer.start();
            var _loc_4:Boolean = false;
            this._rightBtn.enabled = false;
            this._leftBtn.enabled = _loc_4;
            return;
        }// end function

    }
}
